package edu.hawaii.wicket.page.scheduler;

import java.io.Serializable;
/**
 * Handles Options for the calendar.
 * @author Gerolf Seitz
 * https://github.com/gseitz
 * @param <T> Type.
 */
public class Option<T> implements Serializable { 
  private static final long serialVersionUID = 1L;

  private final T defaultValue;

  private final String name;

  private T value;
  /**
   * Constructor.
   * @param name name
   * @param defaultValue value.
   */
  public Option(String name, T defaultValue) { 
    this.name = name;
    this.defaultValue = defaultValue;
  } 
  /**
   * Constructor.
   * @param name name.
   */
  public Option(String name) { 
    this(name, null);
  } 
  /**
   * Check for value.
   * @return state of value.
   */
  public boolean hasValue() { 
    return (defaultValue != null && value != null && !defaultValue.equals(value)) || 
    value != null;
  } 
  /**
   * Returns the value.
   * @return value.
   */
  public T getValue() { 
    return value;
  } 
  /**
   * Sets the value.
   * @param value value.
   */
  public void setValue(T value) { 
    this.value = value;
  } 
 /**
  * Returns the name.
  * @return name.
  */
  public String getName() { 
    return name;
  } 
  /**
   * Conversion to JsonString.
   * @return JsonString.
   */
  public String asJsonString() { 
    if (value instanceof JsonRenderable) { 
      return ((JsonRenderable)value).asJsonString();
    } 

    StringBuilder sb = new StringBuilder();
    sb.append(name);
    sb.append(":");
    sb.append(value);
    return sb.toString();
  } 

  @Override
  /**
   * Override for hash code.
   */
  public int hashCode() { 
    final int prime = 31;
    int result = 1;
    result = prime * result + ((defaultValue == null) ? 0 : defaultValue.hashCode());
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    result = prime * result + ((value == null) ? 0 : value.hashCode());
    return result;
  } 
 
  @Override
  /**
   * Override for equals().
   * @param obj object to compare.
   */
  public boolean equals(Object obj) { 
    if (this == obj) { 
      return true;
    } 
    if (obj == null) { 
      return false;
    } 
    if (getClass() != obj.getClass()) { 
      return false;
    }
    Option<?> other = (Option<?>)obj;
    if (defaultValue == null) { 
      if (other.defaultValue != null) { 
        return false;
      } 
    } 
    else if (!defaultValue.equals(other.defaultValue)) { 
      return false;
    } 
    if (name == null) { 
      if (other.name != null) { 
        return false;
      } 
    } 
    else if (!name.equals(other.name)) { 
      return false;
    } 
    if (value == null) { 
      if (other.value != null) { 
        return false;
      } 
    }
    else if (!value.equals(other.value)) { 
      return false;
    } 
    return true;
  } 


} 

